<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 头部 -->
        <div class="top">
          <el-button type="primary" @click="addPagesFn">
            添加页面
          </el-button>
          <div class="right">
            <el-input v-model="input" placeholder="请输入关键字" />
            <el-button @click="selectFn">筛选</el-button>
          </div>
        </div>
        <!-- 中间内容 -->
        <div class="content">
          <el-table
            height="calc(100vh - 247px)"
            border=""
            :data="pageLists"
            style="width: 100%"
          >
            <el-table-column
              align="center"
              prop="title"
              label="标题"
              width="380"
            />
            <el-table-column
              align="center"
              prop="content"
              label="内容"
            >
              <!-- 使用插槽可以解析html内容 -->
              <template v-slot="slotProps">
                <div v-html="slotProps.row.content" />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="操作"
              width="380"
            >
              <template v-slot="{row}">
                <!-- :disabled="checkPermission('POINT-USER-UPDATE')" -->
                <el-button type="primary" size="small" @click="editFn(row.id)">编辑</el-button>
                <el-button type="danger" size="small" @click="delFn(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height:50px;">
          <el-pagination
            :page-sizes="[5,10,15,20]"
            :page-size="pagesQuery.limit"
            :current-page="pagesQuery.start"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="changePages"
          />
        </el-row>
        <!-- 新增页面组件 -->
        <addPages ref="addPages" :show-dialog.sync="showDialog" @updateEvent="updateEvent" />
      </el-card>
    </div>
  </div>
</template>

<script>
import { getPagesLists, getPagesCount, delPages } from '@/api/pages'
import addPages from './compoment/add-pages.vue'
export default {
  components: { addPages },
  data() {
    return {
      input: '',
      pagesQuery: {
        limit: 5, // 每页获取10条数据
        start: 0 // 开始请求的数据
      },
      pageLists: [], // 页面列表
      total: 0, // 总数据
      showDialog: false
    }
  },
  created() {
    this.getPagesLists()
  },
  methods: {
    // 封装页面列表数据
    async getPagesLists() {
      // 添加加载中效果
      const loading = this.$loading({
        lock: true,
        text: '拼命加载中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.pageLists = await getPagesLists({
        _limit: this.pagesQuery.limit,
        _start: this.pagesQuery.start
      })
      // 关闭加载中效果
      loading.close()
      this.total = await getPagesCount()
    },
    // 处理每页条数的时候触发的事件
    handleSizeChange(size) {
      this.pagesQuery.limit = size
      this.getPagesLists()
    },
    // 页码变动时候触发的事件
    changePages(page) {
      // 起始数据 = (当前点击的页码数-1) * 每页获取的数据
      this.pagesQuery.start = (page - 1) * this.pagesQuery.limit
      this.getPagesLists()
    },
    // 新增页面
    addPagesFn() {
      this.showDialog = true
    },
    // 子组件数据发生更新就调用页面列表数据
    updateEvent() {
      this.getPagesLists()
    },
    // 删除页面
    delFn(id) {
      this.$confirm('请问是否删除该数据', '删除', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning'
      }).then(async() => {
        await delPages(id)
        // 当删除最后一页的最后一条数据
        // 数组长度等于1代表只有一条数据 并且起始数据大于1
        if (this.pageLists.length === 1 && this.pagesQuery.start > 0) {
          // 需要请求上一页的数据
          this.pagesQuery.start = this.pagesQuery.start - 10
        }
        this.getPagesLists()
        this.$message.success('删除成功！')
      }).catch(() => {
        console.log('取消')
      })
    },
    // 编辑页面
    editFn(id) {
      this.showDialog = true
      // 调用子组件的方法回显数据
      this.$refs.addPages.getPagesDetailsId(id)
    },
    // 筛选数据
    async selectFn() {
      const loading = this.$loading({
        lock: true,
        text: '拼命加载中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })

      this.pageLists = await getPagesLists({
        // 筛选的时候不需要加上_limit和_start参数
        title_contains: this.input
      })
      this.total = this.pageLists.length
      loading.close()
      this.input = ''
    }
  }
}
</script>

<style lang="scss" scoped>
 ::v-deep .top {
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #eee;
  padding-bottom: 20px;
  margin-bottom: 20px;
.right {
  display: flex;
   .el-input {
  width: 600px;
}
 }
}
</style>
